﻿using HCQ2_Common.Constant;
using HCQ2_Model;
using HCQ2_Model.Log;
using HCQ2_Model.ViewModel.WebAPI;
using HCQ2_Model.VR.Params;
using HCQ2_Model.VRDD.InputDto._0623;
using HCQ2_Model.VRDD.InputDto._0629;
using HCQ2_Model.VRDD.InputDto._0714;
using HCQ2_Model.VRDD.InputDto._0730;
using HCQ2_Model.VRDD.InputDto._0806;
using HCQ2_Model.VRDD.InputDto._0818;
using HCQ2_Model.VRDD.OutputDto._0623;
using HCQ2_Model.VRDD.OutputDto._0629;
using HCQ2_Model.VRDD.OutputDto._0714;
using HCQ2_Model.VRDD.OutputDto._0730;
using HCQ2_Model.VRDD.OutputDto._0806;
using HCQ2_Model.VRDD.OutputDto._0818;
using HCQ2UI_Helper.Session;
using HCQ2WebAPI_Logic.BaseAPIController;
using HCQ2WebAPI_Logic.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace HCQ2WebAPI_Logic.DocumentController
{
    /// <summary>
    ///  VR度度圈扫码登录需求
    /// </summary>
    public class VRDDScanController : BaseWeiXinApiLogic
    {
        //****************************************************2021-07-30需求*******************************************************
        #region 1.0 ✔创建扫码登录 + WebApiResultJsonModel CreateScanLogin(InputScanLogin param)
        /// <summary>
        ///  1.0 ✔创建扫码登录
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel CreateScanLogin(InputScanLogin param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(param.userid);
            if(user_id<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "用户内部编码参数异常！", "");
            //2.0 将设备过往数据全部设置为离线
            operateContext.bllSession.T_VRDDScanLogin.Modify(new T_VRDDScanLogin { user_status = false, update_date = DateTime.Now }, s => s.device_id.Equals(param.device_id), "user_status", "update_date");
            //3.0 添加新的扫码登录记录
            int mark = operateContext.bllSession.T_VRDDScanLogin.Add(new T_VRDDScanLogin { device_id = param.device_id, device_name = param.device_name, user_id = user_id, userid = param.userid, create_date = DateTime.Now, update_date = DateTime.Now, user_status = true });
            if(mark>0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "创建扫码登录成功！", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "创建扫码登录失败！", "");
        }
        #endregion

        #region 1.1 ✔获取用户的扫码登录列表 + WebApiResultJsonModel GetUserScanLoginList(InputUserScan param)
        /// <summary>
        ///  1.1 ✔获取用户的扫码登录列表
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel<List<OutputUserScan>> GetUserScanLoginList(InputUserScan param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel<List<OutputUserScan>>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            var list = operateContext.bllSession.T_VRDDScanLogin.GetUserScanLoginList(param);
            if(list==null || list.Count<=0)
                return ApiResultJsonModel<List<OutputUserScan>>.RedirectWebApi(WebResultCode.Exception, "当前用户无扫码登录记录！", null);
            return ApiResultJsonModel<List<OutputUserScan>>.RedirectWebApi(WebResultCode.Ok, "成功获取用户的扫码登录列表！", list);
        }
        #endregion

        #region 1.2 ✔退出扫码登录 + WebApiResultJsonModel UserScanLogOff(InputScanExit param)
        /// <summary>
        ///  1.2 ✔退出扫码登录
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel UserScanLogOff(InputScanExit param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int mark = operateContext.bllSession.T_VRDDScanLogin.SelectCount(s => s.device_id.Equals(param.device_id) && s.userid.Equals(param.userid) && s.user_status);
            if(mark<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前用户对应的设备尚未扫码登录过！", "");
            mark = operateContext.bllSession.T_VRDDScanLogin.Modify(new T_VRDDScanLogin { user_status = false, update_date = DateTime.Now }, s => s.device_id.Equals(param.device_id) && s.userid.Equals(param.userid) && s.user_status, "user_status", "update_date");
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "退出成功！", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "退出失败！", "");
        }
        #endregion

        //****************************************************2021-08-03需求*******************************************************
        #region 2.0 ✔保存用户浏览历史 + WebApiResultJsonModel SaveUserBrowseLog(InputUserbrowse param)
        /// <summary>
        ///  2.0 ✔保存用户浏览历史
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SaveUserBrowseLog(InputUserbrowse param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(param.userid);
            if (user_id <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "用户内部编码参数异常！", "");
            int mark = operateContext.bllSession.T_VRDDUserBrowseLog.Add(new T_VRDDUserBrowseLog
            {
                userid = param.userid,
                user_id = user_id,
                b_type = param.b_type,
                file_id = (param.b_type == 1 || param.b_type == 3) ? param.id : 0,
                folder_id = (param.b_type == 2 || param.b_type == 4) ? param.id : 0,
                b_source = param.b_source,
                create_date = DateTime.Now
            });
            if (mark > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功保存用户浏览历史！", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "保存用户浏览历史失败！", "");
        }
        #endregion

        #region 2.1 ✔获取用户浏览历史 + WebApiResultJsonModel GetUserBrowseList(InputUserBrowseParam param)
        /// <summary>
        ///  2.1 ✔获取用户浏览历史
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel<List<OutputUserBrowse>> GetUserBrowseList(InputUserBrowseParam param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel<List<OutputUserBrowse>>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            var list = operateContext.bllSession.T_DocumentInfo.GetUserBrowse(param);
            return ApiResultJsonModel<List<OutputUserBrowse>>.RedirectWebApi(WebResultCode.Ok, "成功获取用户浏览历史列表！", list);
        }
        #endregion

        #region 2.2 ✔获取我的动态图片列表 + WebApiResultJsonModel GetMyDynamicList(BaseEParam param)
        /// <summary>
        ///  2.2 ✔获取我的动态图片列表
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel<List<OutputMyDynamic>> GetMyDynamicList(BaseEParam param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel<List<OutputMyDynamic>>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = HCQ2UI_Helper.Session.SysPermissSession.ChangeIdByGuid(param.userid);
            if (user_id <= 0)
                return ApiResultJsonModel<List<OutputMyDynamic>>.RedirectWebApi(WebResultCode.Exception, "用户内部编码参数异常！",null);
            var list = operateContext.bllSession.T_VRDDCircleDynamic.GetMyPicDynamic(param, user_id);
            return ApiResultJsonModel<List<OutputMyDynamic>>.RedirectWebApi(WebResultCode.Ok, "成功获取我的动态图片列表！", list);
        }
        #endregion

        //****************************************************2021-08-06需求*******************************************************
        #region 3.0 ✔获取作品体验记录明细 + WebApiResultJsonModel GetVLionsRecordDetial(InputVRRecord param)
        /// <summary>
        ///  3.0 ✔获取作品体验记录明细
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel<OutputVRRecord> GetVLionsRecordDetial(InputVRRecord param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel<OutputVRRecord>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(param.userid);
            if (user_id <= 0)
                return ApiResultJsonModel<OutputVRRecord>.RedirectWebApi(WebResultCode.Exception, "用户内部编码参数异常！", null);
            var list = operateContext.bllSession.T_VRDDScanLogin.GetOutputVRRecord(user_id, param.file_id);
            return ApiResultJsonModel<OutputVRRecord>.RedirectWebApi(WebResultCode.Ok, "成功获取作品体验记录明细！", list);
        }
        #endregion

        #region 3.1 ✔按课程获取作品体验记录 + WebApiResultJsonModel GetRecordByCourse(InputVRWork param)
        /// <summary>
        ///  3.1 ✔按课程获取作品体验记录
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel<OutputRecordCourse> GetRecordByCourse(InputVRWork param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel<OutputRecordCourse>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            var list = operateContext.bllSession.T_VRDDScanLogin.GetRecordCourse(param);
            return ApiResultJsonModel<OutputRecordCourse>.RedirectWebApi(WebResultCode.Ok, "成功获取作品体验记录！", list);
        }
        #endregion

        //****************************************************2021-08-18需求*******************************************************
        #region 4.0 ✔获取店铺关联节点 + WebApiResultJsonModel<List<OutputStoreId>> GetStoreCourseList(InputStoreId param)
        /// <summary>
        ///  4.0 ✔获取店铺关联节点
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel<List<OutputStoreId>> GetStoreCourseList(InputStoreId param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel<List<OutputStoreId>>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            var list = operateContext.bllSession.T_VRDDCircleCourseRelevance.GetStoreCourseList(param);
            return ApiResultJsonModel<List<OutputStoreId>>.RedirectWebApi(WebResultCode.Ok, "成功获取店铺关联节点！", list);
        }
        #endregion
        //****************************************************2021-08-21需求*******************************************************

        #region 4.1 ✔获取店铺目录列表 + WebApiResultJsonModel GetStorePageList(InputStorePage param)
        /// <summary>
        ///  4.1 ✔ 获取店铺目录列表
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        /// <remarks>
        ///  获取店铺的合伙人协议<br/>
        /// </remarks>
        [HttpPost]
        public WebApiResultJsonModel<List<OutputStorePage>> GetStorePageList(InputStorePage param)
        {
            //1.0 验证数据有效性
            if (!ModelState.IsValid)
                return ApiResultJsonModel<List<OutputStorePage>>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(param.userid);
            List<OutputStorePage> list = operateContext.bllSession.T_VRDDScanLogin.GetStorePageList(param, user_id);
            return ApiResultJsonModel<List<OutputStorePage>>.RedirectWebApi(WebResultCode.Ok, "成功获取店铺目录列表！", list);
        }
        #endregion
    }
}